package cc.rengu.igas.share.common.util;

import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SequenceMapper;
import cc.rengu.oltp.service.common.dao.impl.SequenceMapperImpl;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import static java.lang.Integer.parseInt;

public class GlobalEsbUtil {
    /**
     * 获取ESB全局流水号
     *
     * @return ESB全局流水号
     * @throws Exception 异常
     */
    public static String getGlobalTraceNumber() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String globalTraceNumber = xmlTreeUtil.getXmlTreeStringValue(ShareParamConstant.GLOBAL_TRACE_NUMBER);
        if (StringUtil.isEmptyOrNull(globalTraceNumber)) {
            String esbSequenceNumber = xmlTreeUtil.getXmlTreeStringValue(ShareParamConstant.ESB_SEQUENCE_NUMBER);
            if (StringUtil.isEmptyOrNull(esbSequenceNumber)) {
                globalTraceNumber = instId + "04920000" + "00" + xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            } else {
                globalTraceNumber = instId + "04920000" + "0" + DateUtil.getCurrentDateTime("yyyyMMddHHmmss") + esbSequenceNumber;
            }
            xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_TRACE_NUMBER, globalTraceNumber);
        }
        return globalTraceNumber;
    }

    /**
     * 获取ESB全局调用序号
     *
     * @return 全局调用序号
     * @throws Exception 异常
     */
    public static String getGlobalPathNumber() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String globalPathNumber = xmlTreeUtil.getXmlTreeStringValue(ShareParamConstant.GLOBAL_PATH_NUMBER);
        if (StringUtil.isEmptyOrNull(globalPathNumber)) {
            globalPathNumber = "1";
            xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_SEQUENCE_FLAG, "true");
        } else {
            String genGlobalSequenceFlag = xmlTreeUtil.getXmlTreeStringValue(ShareParamConstant.GLOBAL_SEQUENCE_FLAG);
            if ("false".equals(genGlobalSequenceFlag) || StringUtil.isEmptyOrNull(genGlobalSequenceFlag)) {
                xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_SEQUENCE_FLAG, "true");
            } else {
                String multipCallFlag = xmlTreeUtil.getXmlTreeStringValue("MULTIP_CALL_FLAG");
                if (StringUtil.isEmptyOrNull(multipCallFlag) || "false".equals(multipCallFlag)) {
                    globalPathNumber += ".1";
                    xmlTreeUtil.setXmlTreeStringValue("MULTIP_CALL_FLAG", "true");
                } else {
                    int index = globalPathNumber.lastIndexOf(".");
                    if (index < 0) {
                        globalPathNumber += ".1";
                    } else {
                        int pathIndex = parseInt(globalPathNumber.substring(index + 1)) + 1;
                        globalPathNumber = globalPathNumber.substring(0, index) + "." + pathIndex;
                    }
                }
            }
        }
        xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_PATH_NUMBER, globalPathNumber);
        return globalPathNumber;
    }

    /**
     * 生成ESB流水号
     *
     * @return 生成ESB流水号
     * @throws Exception 异常
     */
    public static String generateEsbSeqeuenceNumber(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String esbSequenceNumber = "";
        SequenceMapper sequenceMapper = new SequenceMapperImpl();
        String esbSequenceNum = sequenceMapper.getNextSeqNoBySeqName("esb_seq_num", 8);
        if (!StringUtil.isEmptyOrNull(esbSequenceNum)) {
            esbSequenceNumber = instId + ShareParamConstant.IGAS_ESB_SYS_ID + "000" + esbSequenceNum.substring(0, 2) + "9" + esbSequenceNum.substring(2);
        } else {
            esbSequenceNumber = instId + ShareParamConstant.IGAS_ESB_SYS_ID + "000" + RandomUtil.randomNumStr(2) + "9" + RandomUtil.randomNumStr(6);
        }
        xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.ESB_SEQUENCE_NUMBER, esbSequenceNumber);
        return esbSequenceNumber;
    }
}
